<template>
  <body id="poster">
    <el-form class="login-container" label-position="left" label-width="0px">
      <h3 class="login_title">注册</h3>
         <el-form-item>
        <el-input type="text" v-model="username" auto-complete="off" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="text" v-model="email" auto-complete="off" placeholder="邮箱"></el-input>
      </el-form-item>
 
      <el-form-item>
        <el-input type="password" v-model="password" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="password" v-model="repassword" auto-complete="off" placeholder="确认密码"></el-input>
      </el-form-item>
       </el-form-item>
      <el-form-item>
        <el-input type="text" v-model="phone" auto-complete="off" placeholder="输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="text" v-model="code" auto-complete="off" placeholder="验证码"></el-input>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" style="background: #505458;border: none;" v-on:click="get_mycode">获取验证码</el-button>
      </el-form-item>
 
 
      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 100%;background: #505458;border: none" v-on:click="register">注册</el-button>
      </el-form-item>
    </el-form>
  </body>
</template>
<script>
  import {phone,reg} from '../api/api'
    export default {
        name: "register",
        data() {
            return {
                    username:'',
                    email: '',
                    password: '',
                    repassword:'',
                    phone:'',
                    code:'',

              
            }
        },
        methods: {
           // 获取手机验证码
      get_mycode:function(){
        phone(this.phone).then( res => {
          if ( res.code == 200){
            this.$Message('短信已发送')
          }
        })
      },
      register:function(){
        if(this.username==''){
          this.$Message('用户名不能为空')
          return false;
       
        }


        if(this.email==''){
           this.$Message('邮箱不能为空')
           return false;

         }else {
          var remail=/^[\w\-\.]+@[a-z0-9]+(\-[a-z0-9]+)?(\.[a-z0-9]+(\-[a-z0-9]+)?)*\.[a-z]{2,4}$/i;
          if(remail.test(this.email)==false){
              this.$Message('请输入正确的邮箱地址')
              return false;

            }
        }

        if(this.password!=this.repassword){
          this.$Message('两次密码不一致')
          return false;

        }
        if(this.phone==''){
          this.$Message('手机号不能为空')
          return false;

        }else {
          var myphone = /^[1][3,4,5,7,8,9][0-9]{9}$/;
          if(myphone.test(this.phone)==false){
              this.$Message('请输入有效的手机号')
              return false;

            }

        }
       // 以上为对数据的验证
       var data={'username':this.username,'password':this.password,'email':this.email,'phone':this.phone,'code':this.code}
       reg(data).then( res => {
           if(res.code==200){
            this.$Message(res.message)
            this.$router.push({ path: '/login' })
           }else {
             this.$Message(res.message)
           }
         
        })


      }
            
      }
    }
</script>
 
<style>
  #poster {
    background:url("../../static/img/bg.jpg") no-repeat;
    
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
  }
  body{
    margin: 0px;
    padding: 0;
  }
 
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 90px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
 
  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
 
 
</style>